package org.spring.oss;

import io.minio.*;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MinIoOss implements IOss{
    private MinioClient getClient(){
        return MinioClient.builder()
                .endpoint(getConfig().getEndPoint())
                .credentials(getConfig().getAccessKey(), getConfig().getAccessSecretKey())
                .region(getConfig().getRegion())
                .build();
    }

    private OssConfig ossConfig;
    @Override
    public OssConfig getConfig() {
        return ossConfig;
    }

    @Override
    public void setConfig(OssConfig config) {
        this.ossConfig=config;
    }

    @Override
    public boolean uploadStream(String bucket, String name, String filename, InputStream inputStream, long size) {
        try (MinioClient minioClient=getClient()){
            Map<String,String> userMeta=new HashMap<>();
            userMeta.put("filename",filename);
            PutObjectArgs putObjectArgs = PutObjectArgs
                    .builder()
                    .object(encodeName(name))
                    .userMetadata(userMeta)
                    .stream(inputStream, size, size)
                    .build();
            minioClient.putObject(putObjectArgs);
            return true;
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String buildDownloadUrl(String bucket, String name, long validTime) {
        try (MinioClient minioClient=getClient()){
            GetPresignedObjectUrlArgs getPresignedObjectUrlArgs =
                    GetPresignedObjectUrlArgs.builder()
                            .object(encodeName(name))
                            .expiry((int)validTime, TimeUnit.MINUTES).build();
            return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean bucketExists(String bucket) {
        try(MinioClient minioClient=getClient()){
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean objectExists(String bucket, String name) {
        try(MinioClient minioClient=getClient()){
            return minioClient.statObject(StatObjectArgs.builder()
                            .bucket(bucket)
                            .object(encodeName(name)).build()
            )!=null;
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteObject(String bucket, String name) {
        try(MinioClient minioClient=getClient()){
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket)
                    .object(encodeName(name)).build());
            return true;
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }

}
